#include "achieve.h"
#include "ui_achieve.h"
#include "wode.h"
#include "ui_wode.h"
#include"databasemanager.h"
#include <QSqlQuery>
#include <QSqlError>

Achieve::Achieve(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Achieve)
{
    ui->setupUi(this);
    QSqlQuery query(DatabaseManager::getDatabase());
    query.prepare("select * from user where UID=?;");
    query.addBindValue(DatabaseManager::getUID());
    bool success = query.exec();
    if (success) {
        while (query.next()) {
            QString ID = query.value("ID").toString();
            int focus = query.value("focus").toInt();
            int fruit = query.value("fruit").toInt();
            qDebug() << "User data in loop:" << "ID:" << ID << "FOCUS:" << focus << "FRUIT" << fruit;
            this->ID = ID;
            this->focus = focus;
            this->fruit = fruit;
        }
    }
}

Achieve::~Achieve()
{
    delete ui;
}

void Achieve::on_pushButton_4_clicked()
{
    this->close();
}//退出成就系统

int Achieve::getfruit(){
    return fruit;
}
void Achieve::gainfruit(int focusadd,bool fruitadd){
    qDebug()<<"gainfuit start";
    if(fruitadd){
        fruit++;
    }
    focus+=focusadd;
    QSqlQuery query(DatabaseManager::getDatabase());
    query.prepare("update user set focus=?,fruit=? where UID=?;");
    query.addBindValue(focus);
    query.addBindValue(fruit);
    query.addBindValue(DatabaseManager::getUID());
    query.exec();
}

void Achieve::displayfruit(QLabel*label)const{
    qDebug()<<"fruit:"<<fruit;
    label->setText(QString("@%1").arg(ID));

    label->setStyleSheet(
        "background-color: #F0F0F0; "
        "color: black; "
        "font-family: 'Microsoft YaHei'; "
        "font-size: 20px;");
}
